import 'package:PollOnLine/values/MyColors.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../r.dart';
import 'home_logic.dart';
import 'task_list/task_list_view.dart';
import 'triangle_decoration.dart';

class HomePage extends StatefulWidget {
  final String title;

  const HomePage({Key? key, required this.title}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final logic = Get.put(HomeLogic());
  final state = Get.find<HomeLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFEDF1F5,
        body: Stack(
          children: [
            Image.asset(R.assetsImgBgHome1, fit: BoxFit.fitWidth),
            Column(
              children: [
                SafeArea(
                  child: Container(
                    margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 32.r),
                    child: Row(
                      children: [
                        Image.asset(R.assetsImgIconHome1, fit: BoxFit.fill, height: 22.r),
                        Padding(
                          padding: EdgeInsets.only(left: 9.5.r),
                          child: ClipRRect(
                            borderRadius: BorderRadius.all(Radius.circular(15.r)),
                            child: SizedBox(
                              width: 240.r,
                              height: 30.r,
                              child: BrnSearchText(
                                innerPadding: const EdgeInsets.all(0),
                                maxHeight: 30.r,
                                hintText: "请输入任务关键字",
                                hintStyle: TextStyle(color: MyColors.FF888888, fontSize: 12.r),
                                textStyle: TextStyle(color: MyColors.FF888888, fontSize: 12.r),
                                prefixIcon: Container(
                                    padding: EdgeInsets.only(left: 13.5.r, top: 5.r, bottom: 5.r),
                                    child: Image.asset(R.assetsImgIconHome2, width: 13.r)),
                                innerColor: Colors.white,
                                borderRadius: BorderRadius.all(Radius.circular(10.r)),
                                normalBorder: Border.all(color: Colors.white, width: 1, style: BorderStyle.solid),
                                activeBorder: Border.all(color: Colors.white, width: 1, style: BorderStyle.solid),
                                autoFocus: false,
                                onTextClear: () {
                                  return false;
                                },
                                action: Container(),
                                onActionTap: () {
                                },
                                onTextCommit: (text) {
                                },
                                onTextChange: (text) {
                                },
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 15.r, right: 15.r, top: 27.r),
                  child: TabBar(
                    controller: logic.tabController,
                    padding: EdgeInsets.all(0.r),
                    // 是否自动调整indicatorColor
                    automaticIndicatorColorAdjustment: true,
                    // 指示器
                    indicator: TriangleIndicator(triHeight: 10.r),
                    indicatorColor: Colors.white,
                    indicatorWeight: 5,
                    // indicatorPadding: EdgeInsets.only(left: 30.r, right: 30.r),
                    // indicatorSize: TabBarIndicatorSize.label,
                    // 分割线
                    dividerHeight: 0,
                    dividerColor: Colors.transparent,
                    // 选中文字内边距
                    labelPadding: EdgeInsets.only(left: 0.r, right: 5.r, top: 0.r, bottom: 0.r),
                    isScrollable: true,
                    physics: const BouncingScrollPhysics(),
                    tabAlignment: TabAlignment.start,
                    tabs: [
                      for (var i = 0; i < state.tabs.length; i++)
                        Container(
                          height: 50.r,
                          padding: EdgeInsets.only(left: 8.r, right: 8.r, top: 5.r, bottom: 4.r),
                          decoration: BoxDecoration(
                            color: logic.isChoose(i) ? Colors.white : Colors.white.withOpacity(0.25),
                            borderRadius: BorderRadius.circular(5.r),
                          ),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(state.tabs[i].num, style: TextStyle(color: logic.isChoose(i) ? MyColors.FF1C71E5 : Colors.white, fontSize: 15.r)),
                              Text(state.tabs[i].title,
                                  style: TextStyle(color: logic.isChoose(i) ? MyColors.FF1C71E5 : Colors.white, fontSize: 13.r)),
                            ],
                          ),
                        ),
                    ],
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 7.5.r, bottom: 8.r),
                    child: TabBarView(
                      controller: logic.tabController,
                      children: [for (final tab in state.tabs) TaskListPage(title: '任务列表', tab: tab)],
                    ),
                    // child: PageView(
                    //   controller: logic.pageController,
                    //   children: [for (final tab in state.tabs) TaskListPage(title: '任务列表', tab: tab)],
                    //   onPageChanged: (int index) {
                    //     logic.tabController.animateTo(index);
                    //   },
                    // ),
                  ),
                ),
              ],
            ),
          ],
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<HomeLogic>();
    super.dispose();
  }
}
